package com.example.xyr.myapplication.ui.im;

import android.app.Activity;

import com.example.xyr.myapplication.Constant;
import com.example.xyr.myapplication.model.UserInfoBean;
import com.example.xyr.myapplication.utils.BitmapUtils;
import com.example.xyr.myapplication.utils.LogUtil;
import com.sdsmdg.tastytoast.TastyToast;

import java.io.File;

import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.callback.GetUserInfoCallback;
import cn.jpush.im.android.api.model.UserInfo;
import cn.jpush.im.api.BasicCallback;

/**
 * Created by ylcf on 2018/7/25.
 */

public class ChatUserUtils {

    /**
     * 获取用户信息
     *
     * @param username
     * @param callback
     */
    public static void getUserInfo(final String username, final UserInfoCallback callback) {
        JMessageClient.getUserInfo(username, Constant.APPKEY, new GetUserInfoCallback() {
            @Override
            public void gotResult(int i, String s, UserInfo userInfo) {
                if (i == 0) {
                    callback.gotResult(userInfo);
                } else {
                    LogUtil.e("tag", "获取用户信息 失败 ");
                    callback.errMsg(s);
                }
            }
        });
    }

    public static void getOtherNickName(String username, final UserNickNameCallback callback) {
        JMessageClient.getUserInfo(username, Constant.APPKEY, new GetUserInfoCallback() {
            @Override
            public void gotResult(int i, String s, UserInfo userInfo) {
                if (i == 0) {
                    callback.gotResult(userInfo);
                } else {
                    LogUtil.e("tag", "获取用户信息 失败 ");
                    callback.errMsg(s);
                }
            }
        });
    }

    public interface UserInfoCallback {
        void gotResult(UserInfo userInfo);

        void errMsg(String err);
    }

    public interface UserNickNameCallback {
        void gotResult(UserInfo userInfo);

        void errMsg(String err);
    }

    /**
     * 更新昵称
     *
     * @param nickName
     */
    public static void updateNickName(final Activity activity, String nickName) {
        UserInfoBean userInfo = new UserInfoBean();
        userInfo.setNickname(nickName);
        JMessageClient.updateMyInfo(UserInfo.Field.nickname, userInfo, new BasicCallback() {
            @Override
            public void gotResult(int i, String s) {
                if (i == 0) {
                    TastyToast.makeText(activity, "更新爱称成功 ", TastyToast.LENGTH_LONG, TastyToast.SUCCESS);
                } else {
                    TastyToast.makeText(activity, "更新爱称失败 = " + s, TastyToast.LENGTH_LONG, TastyToast.ERROR);
                }
            }
        });
    }

    /**
     * 更新用户位置
     */
    public static void updateAddRess(String address) {
        UserInfoBean userInfo = new UserInfoBean();
        userInfo.setAddress(address);
        JMessageClient.updateMyInfo(UserInfo.Field.address, userInfo, new BasicCallback() {
            @Override
            public void gotResult(int i, String s) {
                if (i == 0) {
                } else {
                    LogUtil.e("tag", "位置更新失败");
                }
            }
        });
    }

    public static void updateGender(final Activity activity, UserInfo.Gender gender) {
        UserInfoBean userInfo = new UserInfoBean();
        userInfo.setGender(gender);
        JMessageClient.updateMyInfo(UserInfo.Field.gender, userInfo, new BasicCallback() {
            @Override
            public void gotResult(int i, String s) {
                if (i == 0) {
                } else {
                    TastyToast.makeText(activity, "性别更新成功", TastyToast.LENGTH_LONG, TastyToast.SUCCESS);
                    LogUtil.e("tag", "性别更新失败");
                }
            }
        });
    }

    public static void upDataAvatar(final Activity activity, File avatar) {
        JMessageClient.updateUserAvatar(avatar, new BasicCallback() {
            @Override
            public void gotResult(int i, String s) {
                if (i == 0) {
                    TastyToast.makeText(activity, "更新头像成功= ", TastyToast.LENGTH_LONG, TastyToast.SUCCESS);
                    BitmapUtils.getInstance().upDateUserInfo();
                } else {
                    TastyToast.makeText(activity, "更新头像失败 = " + s, TastyToast.LENGTH_LONG, TastyToast.ERROR);
                }
            }
        });
    }

}
